package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.mapper.CityAreaMapper;
import com.weixing.mall.provider.model.domain.CityArea;
import com.weixing.mall.provider.model.dto.cms.CityAreaDto;
import com.weixing.mall.provider.model.query.CityAreaQuery;
import com.weixing.mall.provider.model.vo.cms.CityAreaCascaderVo;
import com.weixing.mall.provider.model.vo.cms.CityAreaTreeVo;
import com.weixing.mall.provider.model.vo.cms.CityAreaVo;
import com.weixing.mall.provider.service.ICityAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CityAreaServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/7/26
 * @Version 1.0
 **/

/**
 * <p>
 * CMS模块-行政区划表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class CityAreaServiceImpl extends ServiceImpl<CityAreaMapper, CityArea> implements ICityAreaService {


    @Override
    public IPage<CityAreaVo> selectOfPage(Page page, CityAreaQuery cityAreaQuery) {
        return baseMapper.selectListOfPage(page, cityAreaQuery);
    }

    @Override
    public List<CityAreaVo> selectList(CityAreaQuery cityAreaQuery) {
        return baseMapper.selectListOfPage(cityAreaQuery);
    }

    @Override
    public List<CityAreaTreeVo> selectTreeList() {
        List<CityAreaTreeVo> srcCityAreaTreeVo = baseMapper.selectCityAreaTreeVoAll();
        List<CityAreaTreeVo> distCityAreaTreeVo = Lists.newArrayList();
        CityAreaTreeVo rootCityAreaTreeVo = srcCityAreaTreeVo.get(0);
        fillCityAreaTreeVo(distCityAreaTreeVo, srcCityAreaTreeVo, rootCityAreaTreeVo);
        distCityAreaTreeVo.add(rootCityAreaTreeVo);
        return distCityAreaTreeVo;
    }

    @Override
    public void save(CityAreaDto cityAreaDto) {
        CityArea cityArea = new CityArea();
        BeanUtils.copyProperties(cityAreaDto, cityArea);
        cityArea.setAreaLevel(cityAreaDto.getAreaLevel().getValue());
        if (null == cityAreaDto.getId()) {
            save(cityArea);
            Long id = cityArea.getId();
            if (cityAreaDto.getAreaPath() == "") {
                cityArea.setAreaPath("" + id);
            } else {
                cityArea.setAreaPath(cityAreaDto.getAreaPath() + "|" + id);
            }
            updateById(cityArea);
        } else {
            cityArea.setUpdatedTime(new Date());
            updateById(cityArea);
        }
    }

    @Override
    public List<CityAreaCascaderVo> selectCityAreaCasaderVo() {
        List<CityAreaCascaderVo> srcCityVo = baseMapper.selectCityAreaCasaderVo();
        List<CityAreaCascaderVo> disCityVo = Lists.newArrayList();
        if (!srcCityVo.isEmpty()) {
            List<CityAreaCascaderVo> rootCityVo = baseMapper.selectCityAreaList();
            fillSysDeptCascaderVo(disCityVo, srcCityVo, rootCityVo);
            for (CityAreaCascaderVo cityAreaCascaderVo : rootCityVo) {
                disCityVo.add(cityAreaCascaderVo);
            }
        }
//        System.out.println(disCityVo.get(0).toString());
        return disCityVo;
    }

    /**
     * @Description 递归封装菜单Tree结构 <br/>
     * @author gjq
     * @date 2019/7/30 16:47
     * @param dist
     * @Param: src
     * @Param: root
     * @return void
     */
    private void fillCityAreaTreeVo(List<CityAreaTreeVo> dist, List<CityAreaTreeVo> src, CityAreaTreeVo root) {
        List<CityAreaTreeVo> cityAreaTreeVoList = src.stream().filter((CityAreaTreeVo m1) -> m1.getParentCode().equalsIgnoreCase(root.getAreaCode())).collect(Collectors.toList());
        if (!cityAreaTreeVoList.isEmpty()) {
            root.setChildren(cityAreaTreeVoList);
            src.removeAll(cityAreaTreeVoList);
            for (CityAreaTreeVo m : cityAreaTreeVoList) {
                fillCityAreaTreeVo(dist, src, m);
            }
        }
    }

    /***
     *Description: 递归封装菜单Tree结构 <br/>
     *
     * @param src  源集合
     * @param root 跟目录
     * @param:dist 目标集合
     * @return:
     */
    private void fillSysDeptCascaderVo(List<CityAreaCascaderVo> dist, List<CityAreaCascaderVo> src, List<CityAreaCascaderVo> root) {
        for (CityAreaCascaderVo cityAreaCascaderVo : root) {
            List<CityAreaCascaderVo> menuTreeVoList = src.stream().filter((CityAreaCascaderVo m1) -> m1.getParentCode().equals(cityAreaCascaderVo.getAreaCode())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(menuTreeVoList)) {
                cityAreaCascaderVo.setChildren(menuTreeVoList);
                src.removeAll(menuTreeVoList);
                fillSysDeptCascaderVo(dist, src, menuTreeVoList);
            }
        }
    }
}
